﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Rg.Plugins.Popup.Extensions;
using System.Threading.Tasks;
using TabbedTemplate.Services;
using TabbedTemplate.Views;
using Xamarin.Forms;

namespace TabbedTemplate.ViewModels
{
    public class ImageViewModel : ViewModelBase
    {
        // ******** 绑定属性

        public byte[] ImageSource
        {
            get => _imageSource;
            set => Set(nameof(ImageSource), ref _imageSource, value);
        }
        private byte[] _imageSource;


        //PunchViewPageViewModel
        private byte[] _imageSource1;

        public byte[] ImageSource1
        {
            get => _imageSource1;
            set => Set(nameof(ImageSource1), ref _imageSource1, value);
        }


        /******** 构造函数 ********/

        /// <summary>
        ///添加页面ViewModel。
        /// </summary>
        private readonly PunchPageViewModel _punchPageViewModel;

        private readonly PunchViewPageViewModel _punchViewPageViewModel;

        public ImageViewModel(PunchPageViewModel punchPageViewModel,
            PunchViewPageViewModel punchViewPageViewModel)
        {
            _punchPageViewModel = punchPageViewModel;
            _punchViewPageViewModel = punchViewPageViewModel;
            if (_punchPageViewModel != null)
            {
                ImageSource = _punchPageViewModel.ImageSource;
            }
            else if (_punchViewPageViewModel != null)
            {
                ImageSource1 = _punchViewPageViewModel.ImageSource;
            }
        }

        //处理PunchPageViewModel
        public RelayCommand AddCommand =>
            _addCommand ??=
                new RelayCommand(async () => await AddCommandFunction());

        private RelayCommand _addCommand;

        public async Task AddCommandFunction()
        {
            await _punchPageViewModel.AddImageViewModel(this);
        }

        //处理PunchViewPageViewModel
        public RelayCommand AddCommand1 =>
            _addCommand1 ??=
                new RelayCommand(async () => await AddCommandFunction1());

        private RelayCommand _addCommand1;

        public async Task AddCommandFunction1()
        {
            await _punchViewPageViewModel.AddImageViewModel(this);
        }

        /// <summary>
        /// 删除命令。
        /// </summary>
        public RelayCommand RemoveCommand =>
            _removeCommand ??= new RelayCommand(RemoveCommandFunction);

        private RelayCommand _removeCommand;

        public void RemoveCommandFunction() =>
            _punchPageViewModel.RemoveImageViewModel(this);


        //PunchViewPageViewModel删除
        public RelayCommand RemoveCommand1 =>
            _removeCommand1 ??= new RelayCommand(RemoveCommandFunction1);

        private RelayCommand _removeCommand1;

        public void RemoveCommandFunction1() =>
            _punchViewPageViewModel.RemoveImageViewModel(this);

        //PunchPage查看图片
        public RelayCommand SeeDetailCommand =>
            _seeDetailCommand ??= new RelayCommand(async () => await SeeDetailCommandFunction());

        private RelayCommand _seeDetailCommand;

        public async Task SeeDetailCommandFunction()
        {
            var page = new ImagePopUpPage();
            NavigationContext.SetParameter(page, ImageSource);
            var index = MainPage.Children.IndexOf(MainPage.CurrentPage);
            await MainPage.Children[index].Navigation.PushPopupAsync(page);
        }



        //PunchViewPage查看图片
        public RelayCommand SeeDetailCommand1 =>
            _seeDetailCommand1 ??= new RelayCommand(async () => await SeeDetailCommandFunction1());

        private RelayCommand _seeDetailCommand1;

        public async Task SeeDetailCommandFunction1()
        {
            var page = new ImagePopUpPage();
            NavigationContext.SetParameter(page, ImageSource1);
            var index = MainPage.Children.IndexOf(MainPage.CurrentPage);
            await MainPage.Children[index].Navigation.PushPopupAsync(page);
        }
        public MainPage MainPage =>
            _mainPage ?? (_mainPage = Application.Current.MainPage as MainPage);

        private MainPage _mainPage;
    }
}
